package com.renchunlin.customtitleview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View


/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/5/17.
 * PS: Not easy to write code, please indicate.
 */
class CustomTitleView : View {

    /**
     * 文本
     */
    private var mTitleText: String = ""

    /**
     * 文本的颜色
     */
    private var mTitleTextColor = 0

    /**
     * 文本的大小
     */
    private var mTitleTextSize = 0

    /**
     * 绘制时控制文本绘制的范围
     */
    private var mBound: Rect
    private var mPaint: Paint

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val typeArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView)

        mTitleText = typeArray.getString(R.styleable.CustomTitleView_titleText)!!
        mTitleTextColor =
            typeArray.getColor(R.styleable.CustomTitleView_titleTextColor, Color.BLACK)
        mTitleTextSize = typeArray.getDimensionPixelSize(
            R.styleable.CustomTitleView_titleTextSize,
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16f, resources.displayMetrics)
                .toInt()
        )

        typeArray.recycle()

        /**
         * 绘制文本宽高
         */
        mPaint = Paint()
        mPaint.textSize = mTitleTextSize.toFloat()
        mBound = Rect()
        mPaint.getTextBounds(mTitleText, 0, mTitleText.length, mBound)

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)

        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        var width = widthSize
        var height = heightSize
        if (widthMode == MeasureSpec.AT_MOST) {
            width = paddingLeft + mBound.width() + paddingRight
        }

        if (heightMode == MeasureSpec.AT_MOST) {
            height = paddingTop + mBound.height() + paddingBottom
        }
        setMeasuredDimension(width, height)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //mPaint.color = Color.YELLOW
        //canvas?.drawRect(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat(), mPaint)
        //Log.i("chen", "宽:$measuredWidth \t 高：$measuredHeight")
        mPaint.color = mTitleTextColor
        val fontMetricsInt = mPaint.fontMetricsInt
        val baseline = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom
        canvas?.drawText(
            mTitleText,
            (measuredWidth / 2).toFloat() - (mBound.width())/2,
            measuredHeight / 2 + baseline.toFloat(),
            mPaint
        )
    }

}